﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.BasicData
{
    public partial class EditEmployee :BasePage
    {
        private IDepartmentBLL departmentBLL = IocContainer.Instance.Resolve<IDepartmentBLL>();
        private IEmployeeBLL employeeBLL = IocContainer.Instance.Resolve<IEmployeeBLL>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                DataTable dtDepartment = departmentBLL.GetAllDepartment();
                this.ddlDepartment.Items.Add(new ListItem("【请选择所属部门】", "-1"));
                //this.ddlBR.Items.Add(new ListItem("【请选择所属机构】", "-1"));
                //BindTreeViewToDropdownList(dtDepartment, 0, this.ddlBR, -1);
                BindTreeViewToDropdownList(dtDepartment, 0, this.ddlDepartment, -1, "DepartmentName", "DepartmentId");

                if ("update" == Request.QueryString["Type"].ToString().ToLower())
                {
                    int empId = 0;
                    int.TryParse(Request.QueryString["EmpId"], out empId);
                    if (empId > 0)
                    {
                        Skyworth.CRM.Service.Entity.Employee employee = employeeBLL.GetEmployeeById(empId);
                        this.txtEmpName.Text = employee.EmpName;
                        this.txtEmpCode.Text = employee.EmpCode;
                        this.ddlDepartment.SelectedValue = employee.DeptId.ToString();
                        //this.ddlBR.SelectedValue = employee.BrId.ToString();
                        this.txtBankName.Text = employee.BankName;
                        this.txtBankNumber.Text = employee.BankNumber;
                        this.txtJobTitle.Text = employee.JobTitle;
                        this.txtEmail.Text = employee.Email;
                        this.txtPhone.Text = employee.Phone;
                        
                        int status = 0;
                        if (employee.Status) status = 1;
                        this.ddlStatus.SelectedValue = status.ToString();

                    }
                }
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Boolean isOK = false;
                Skyworth.CRM.Service.Entity.Employee employee = new Skyworth.CRM.Service.Entity.Employee();
                employee.EmpCode =txtEmpCode.Text.Trim();
                employee.EmpName = txtEmpName.Text.Trim();
                employee.Email = txtEmail.Text.Trim();
                employee.JobTitle = txtJobTitle.Text.Trim();
                employee.Phone = txtPhone.Text.Trim();
                employee.DeptId =Convert.ToInt32(ddlDepartment.SelectedValue);
                employee.BankName = txtBankName.Text.Trim();
                employee.BankNumber = txtBankNumber.Text.Trim();
                //employee.BrId = Convert.ToInt32(ddlBR.SelectedValue);
                Boolean status = true;
                if (this.ddlStatus.SelectedValue == "0") status = false;
                employee.Status = status;


                if ("add" == Request.QueryString["Type"].ToString().ToLower())
                {
                        employeeBLL.Add(employee);
                        isOK = true;
                  }
                else if ("update" == Request.QueryString["Type"].ToString().ToLower())
                {
                    int empId = 0;
                    int.TryParse(Request.QueryString["EmpId"], out empId);
                    employee.EmpId =empId;
                    employeeBLL.Update(employee);
                        isOK = true;
                }

                if (isOK)
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "closeThisPage", "<script language=\"JavaScript\">parent.addWindow.close();parent.location.href = parent.location.href;</script>");
                }
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                Alert("发生异常：\r\n" + exception.Message);
            }

        }
    }
}
